﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BusinessLogic.ClassFactory;
using Common;
using Util.PageInfo;
using ValidateCommon;
using Util.BasePage;
using BusinessLogic.BusinessRules;

namespace RichECMS.ServerPages.Section
{
    public partial class Update : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Hiddenpathmain.Value = "../../news/" + State.LoginUser.VartualPath + "/Pcfiles/";
            if (!IsPostBack)
            {
                if (Request["Section"] != null)
                {
                    if (ParamAuth.IsNumber(Request["Section"].ToString()))
                    {
                        State.intSTID = int.Parse(Request["Section"].ToString());
                        ShowPageInfo(State.intSTID);
                        LitPaagePath.Text = PageInfo.ShowPagePath(State.intSTID);
                    }
                }
            }
        }

        private void ShowPageInfo(int intSectionId)
        {
            try
            {
                using (SectionsFactory objFactory = new SectionsFactory())
                {
                    BusinessLogic.BusinessRules.Sections objSection = objFactory.Creator(intSectionId);
                    if (objSection != null)
                    {
                        LitPageName.Text = objSection.DataItem.chvSTCName;
                        this.txtinfo.Value = objSection.DataItem.txtSTMemo;
                        this.txtContent.Value = objSection.DataItem.chvContent.Replace("\"../Pcfiles/", "\"/news/" + State.LoginUser.VartualPath + "/Pcfiles/");
                        this.txtseotitle.Value = objSection.DataItem.txtSeoTitle;
                        this.txtseokeywords.Value = objSection.DataItem.txtSeoKeywords;
                        this.txtseodescription.Value = objSection.DataItem.txtSeodescription;
                        this.TextRedirectUrl.Value = objSection.DataItem.IschvRedirectUrlNull()?"": objSection.DataItem.chvRedirectUrl;
                        DataRow row = objSection.Data.Tables[0].Select()[0];
                        if (row["chvSectionMainPic"] != DBNull.Value)
                        {
                            this.ImgShow.Src = objSection.DataItem.chvSectionMainPic.Trim().Length == 0
                                                  ? Hiddenpathmain.Value + "default.jpg"
                                                   : Hiddenpathmain.Value + objSection.DataItem.chvSectionMainPic;
                        }
                        else
                        {
                            this.ImgShow.Src = Hiddenpathmain.Value + "default.jpg";
                            Imghidden.Value = "";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        protected void SubmitBtnHander(object sender, ImageClickEventArgs e)
        {
            try
            {
                using (SectionsFactory objFactory = new SectionsFactory())
                {
                    BusinessLogic.BusinessRules.Sections objSection = objFactory.Creator(State.intSTID);
                    if (objSection != null)
                    {
                        if (Imghidden.Value.Length != 0)
                        {
                            objSection.DataItem.chvSectionMainPic = Imghidden.Value;
                        }
                        //objSection.DataItem.txtSTMemo = SqlCommon.SqlStringFilter(this.txtinfo.Value);
                        //objSection.DataItem.chvContent = SqlCommon.SqlStringFilter(this.txtContent.Value);
                        objSection.DataItem.txtSTMemo = this.txtinfo.Value;
                        objSection.DataItem.chvRedirectUrl = this.TextRedirectUrl.Value.Trim();
                        objSection.DataItem.chvContent = this.txtContent.Value.Replace("\"/news/" + State.LoginUser.VartualPath + "/Pcfiles/", "\"../Pcfiles/");
                        objSection.DataItem.txtSeoTitle = this.txtseotitle.Value.Trim().Length == 0 ? "    " : this.txtseotitle.Value;
                        objSection.DataItem.txtSeoKeywords = this.txtseokeywords.Value.Trim().Length == 0 ? "    " : this.txtseokeywords.Value;
                        objSection.DataItem.txtSeodescription = this.txtseodescription.Value.Trim().Length == 0 ? "    " : this.txtseodescription.Value;
                        objSection.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                        objSection.Update();
                        AlertAndRedirect("修改成功！", "../Section/update.aspx?Section=" + State.intSTID.ToString(), false);
                    }
                }

            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }
    }
}